package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.JsonResult;
import com.wuming.house.core.WumingRole;
import com.wuming.house.model.Decorate;
import com.wuming.house.model.Information;
import com.wuming.house.model.Member;
import com.wuming.house.service.DecorateService;
import com.wuming.house.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author xuhuai
 * @createTime 2019/8/27 18:51
 * @Description:
 */
@Controller
@RequestMapping("/web/decorate")
public class DecorateController extends BaseController{

    @Autowired
    private DecorateService decorateService;

    @Autowired
    private MemberService memberService;

    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public CommonResult list(@RequestParam(required = false)String title, @RequestParam(required = false)String source,@RequestParam Integer memberId,
                             @RequestParam(defaultValue = "1")int pageIndex, @RequestParam(defaultValue = "10")int pageSize){
        logger.info("准备查询装修案例");
        try {
            Member member = memberService.getMemberById(memberId);
            if (member.getRole()== WumingRole.WUMING_USERCENTER_ROLE_ADMINISTRATOR.ordinal()){
                List<Decorate> decorates = decorateService.getDecoratesByPage(title,source,pageIndex,pageSize);
                int total = decorateService.getTotal(title,source);
                return CommonResult.createSuccessResult(decorates,total,"查询装修案例成功");
            }
            List<Decorate> decorates = decorateService.getDecoratesByMember(title,source,memberId,pageIndex,pageSize);
            int total = decorateService.getTotalByMember(title,source,memberId);
            return CommonResult.createSuccessResult(decorates,total,"查询装修案例成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("发生业务异常,异常信息:{}",e.getMessage());
        }
        return CommonResult.createFailureResult("查询装修案例失败");
    }

    @RequestMapping("/toManageDecorate")
    public String toManageDecorate(){
        return "/tools/manageDecorate";
    }

    @RequestMapping("/toEditDecorate/{type}/{id}")
    public String toEditDecorate(@PathVariable Integer type, @PathVariable Integer id, Model model,@RequestParam Integer memberId){
        model.addAttribute("memberId",memberId);
        if (type==2){
            if (null!=id){
                model.addAttribute("decorate",decorateService.getDecorateById(id));
            }
        }
        return "/tools/editDecorate";
    }

    @ResponseBody
    @RequestMapping(value = "/edit")
    public JsonResult edit(Decorate decorate){
        if (null == decorate.getId()) {
            decorate.setCreateDatetime(new Date());
            decorateService.saveDecorate(decorate);
            return new JsonResult(null,"添加装修案例成功");
        } else {
            decorateService.modifyDecorate(decorate);
            return new JsonResult(null,"更新装修案例成功");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public JsonResult delete(@PathVariable Integer id){
        logger.info("准备删除id:{}的装修案例",id);
        try {
            decorateService.delete(id);
            return new JsonResult(null,"删除成功",true);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("发生业务异常,异常信息:{}",e.getMessage());
            return new JsonResult(null,"删除失败",false);
        }
    }

    @ResponseBody
    @RequestMapping("/uploadImg")
    public JsonResult uploadImg(MultipartFile file, HttpServletRequest request){
        try {
            String path = this.uploadImage(file,"decorate",1);
            logger.info("文件成功上传到指定目录下,文件目录:{},",path);
            return new JsonResult(path,"上传成功",true);
        }catch (IOException ie){
            ie.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult("","上传失败",false);
    }

    @ResponseBody
    @RequestMapping("/get/{decorateId}")
    public JsonResult get(@PathVariable Integer decorateId){
        try {
            Decorate decorate = decorateService.getDecorateById(decorateId);
            return new JsonResult(decorate,"",true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(null,"",false);
    }

}
